package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func swapPairs(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}
	hair := &ListNode{Next: head}
	n := hair
	for n.Next != nil && n.Next.Next != nil {
		n1 := n.Next
		n2 := n1.Next
		n.Next = n1.Next
		n1.Next = n2.Next
		n2.Next = n1
		n = n1
	}
	return hair.Next
}

func main() {
	n1 := &ListNode{Val: 1}
	n2 := &ListNode{Val: 2}
	n3 := &ListNode{Val: 3}
	n4 := &ListNode{Val: 4}

	n1.Next = n2
	n2.Next = n3
	n3.Next = n4
	r := swapPairs(n1)
	println(r)
}
